package jimo.bill.controller;

import jimo.bill.pojo.Bill;
import jimo.bill.pojo.BillType;
import jimo.bill.service.BillService;
import jimo.bill.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private TypeService typeService;
    @Resource
    private BillService billService;

    /*** 查询
     *  @param b
     *  @param model
     *  @return
     *  */
    @RequestMapping("/list")
    public String list(Bill b, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        List<Bill> list = billService.list(b);
        model.addAttribute("list", list);
        model.addAttribute("bill", b);
        return "/bill/list";
    }

    /*** 调到添加页面 * @return */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        return "/bill/add";
    }

    /*** 添加 * @param b * @return */
    @RequestMapping("/add")
    public String add(Bill b) {
        System.out.println("前端添加的bill------->>>"+b);
        billService.add(b);
        return "redirect:/bill/list";
    }

    /*** 删除 * @param id * @return */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        billService.delete(id);
        return "redirect:/bill/list";
    }

    /*** 修改 * @param id * @return */
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Long id, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        Bill bill = billService.get(id);
        model.addAttribute("bill", bill);
        return "/bill/update";
    }

    /*** 修改 * @param b * @return */
    @RequestMapping("/update")
    public String update(Bill b) {
        billService.update(b);
        return "redirect:/bill/list";
    }
}
